<script>
    function getDiffTime(time1, time2) {

//1.计算两个时间相差的毫秒数
var diff = time1 - time2;
diff = Math.abs(diff);//取绝对值
// console.log(diff);

//2.根据相差的毫秒数进行换算

var oneDay = 1000 * 60 * 60 * 24; //一天所需要的毫秒数

var oneHours = 1000 * 60 * 60 //一个小时所需要的毫秒数

var oneMinutes = 1000 * 60 //一分钟所需要的毫秒数

var oneSeconds = 1000; //1秒钟所需要的毫秒数

//计算相差多少天,用相差的毫秒数,除以一天的毫秒数;
var diffDay = diff / oneDay;
//天数取整

diffDay = parseInt(diffDay)

//计算差多少小时

//把总时差和一天的余数计算出来

var yTime = diff % oneDay;


//将剩余的毫秒数转换成小时

var diffHours = yTime / oneHours;
diffHours = parseInt(diffHours)

//取求完小时之后剩下的余数

var yTime2 = yTime % oneHours

//将取余的毫秒数转换成 分

var diffMinutes = yTime2 / oneMinutes;


diffMinutes = parseInt(diffMinutes)
// console.log(diffDay);

//求分钟之后剩下的余数

var yTime3 = yTime2 % oneMinutes;


//求差多少秒
var diffSeconds = yTime3 / oneSeconds;

// console.log(diffSeconds);

diffSeconds = parseInt(diffSeconds)

var res = diffDay + "天" + diffHours + "小时" + diffMinutes + "分" + diffSeconds + "秒"


return res;
}

var time1 = new Date("2008-05-12 17:20:10");

var time2 = new Date()

console.log(getDiffTime(time1, time2));
</script>